package com.uenpay.im.ui.old.activity.phonebook;

import com.qiyunxin.android.http.utils.StringUtils;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-26 下午4:00:48
 * @copyRight http://www.qiyunxin.com
 * @function 上传手机联系人
 */
public class UploadContactsByPhoneBookInvokeItem extends BaseHttpInvokeItem {

	public class UploadContactsByPhoneBookInvokeItemResult extends
			HttpInvokeResult {
		public ArrayList<MobileContatcsEntity> arrayList = new ArrayList<MobileContatcsEntity>();
	}

	public UploadContactsByPhoneBookInvokeItemResult getOutPut() {
		return (UploadContactsByPhoneBookInvokeItemResult) GetResultObject();
	}

	public UploadContactsByPhoneBookInvokeItem(String mobiles) {
		// http://api.qiyunxin.com/Friend/Friend/checkByMobile?mobiles=
		String url = APIConfiguration.getBaseUrl()
				+ "Friend/Friend/checkByMobile?mobiles="
				+ StringUtils.encodeParams(mobiles) + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		UploadContactsByPhoneBookInvokeItemResult result = new UploadContactsByPhoneBookInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}

		JSONArray arr = json.optJSONArray("data");
		if (arr != null) {
			for (int i = 0, length = arr.length(); i < length; i++) {
				result.arrayList.add(desMobileContatcsEntity(arr
						.optJSONObject(i)));
			}

		}
		return result;
	}

	private MobileContatcsEntity desMobileContatcsEntity(JSONObject json) {
		MobileContatcsEntity mMobileContatcsEntity = new MobileContatcsEntity();
		mMobileContatcsEntity.mobilePhone = json.optString("mobile");
		mMobileContatcsEntity.nickName = json.optString("nickname");
		mMobileContatcsEntity.custId = json.optString("custid");
		mMobileContatcsEntity._isfriend = json.optInt("_isfriend");
		mMobileContatcsEntity._invited = json.optInt("_invited");
		return mMobileContatcsEntity;
	}

}
